import { defineStore } from "pinia";
import { getUserSettings } from "../request/api/ecard";

/** 系统 */
export const useECardStore = defineStore('ecard', {
	state: () => ({
		settings: [], // 配置项
		active: 0, // 激活配置项索引
	}),
	actions: {
		hold(data = []) {
			this.settings = data
		},
		clear() {
			this.settings = []
		},
		/**
		 * 加载用户电子名片配置
		 * @param {String} userId
		 */
		loadUserSettings(userId) {
			if (!userId) return Promise.reject({ code: -1, message: '参数错误' })
			return new Promise((resolve, reject) => {
				getUserSettings(userId).then(data => {
					this.hold(data)
					resolve(this.settings)
				}).catch(({ code = -1, message = '数据加载失败' }) => {
					reject({ code, message })
				})
			})
		},
		/**
		 * 激活配置项
		 * @param {Number} index
		 */
		activeSetting(index) {
			this.active = index
		}
	}
})